import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Leetcode429 {
    public List<List<Integer>> levelOrder(Node root) {
        List<List<Integer>> res = new ArrayList<>();
        levelOrder(root, 0, res);

        return res;
    }

    private void levelOrder(Node root, int level, List<List<Integer>> res) {
        if (root == null) {
            return;
        }

        if (level == res.size()) {
            res.add(new ArrayList<>());
        }

        res.get(level).add(root.val);

        ++level;
        for (Node child : root.children) {
            levelOrder(child, level, res);
        }
    }

    class Node {
        public int val;
        public List<Node> children;

        public Node() {
        }

        public Node(int _val) {
            val = _val;
        }

        public Node(int _val, List<Node> _children) {
            val = _val;
            children = _children;
        }
    }

    ;
}

